package net.kleinhenz.questor;

import java.net.URL;

import net.kleinhenz.questor.game.Game;
import net.kleinhenz.questor.system.QSRFile;
import net.kleinhenz.questor.ui.SettingsActivity;
import net.kleinhenz.questor.ui.StoredStorySelectorActivity;
import net.kleinhenz.questor.ui.StoryActivity;
import net.kleinhenz.questor.ui.StorySelectorActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.View.OnClickListener;
import android.widget.ImageView;

/**
 * This is the main view and serves as a starting point
 * into games.
 * 
 * @author Michael Kleinhenz, m.kleinhenz@tarent.de
 */
public class Questor extends Activity implements OnClickListener, OnMenuItemClickListener
{	
	// application data
	public static final String APP_NAME = "Questor";
	
	public static final int REQ_STORYSELECT = 0;
	public static final int REQ_STORYLOAD = 1;

	private static Game currentGame = null;
	private static QSRFile loadedQSR = null;
	
	// widgets
	@SuppressWarnings("unused")
	private Menu menu = null;
	private ImageView menuButton = null;
	private MenuItem itemStart = null;
	private MenuItem itemLoad = null;
	private MenuItem itemSettings = null;
	private MenuItem itemClose = null;

	// activities
	private Intent storySelectorActivity = null;
	private Intent storedStorySelectorActivity = null;
		
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
    	// create activity
        super.onCreate(savedInstanceState);        
		requestWindowFeature(Window.FEATURE_NO_TITLE);  
        setContentView(R.layout.main);

        // retrieve widget instances, adding listeners
        menuButton = (ImageView)findViewById(R.id.mainMenuButton);
        menuButton.setOnClickListener(this);        
    }

	public static void setCurrentGame(Game currentGame)
	{
		Questor.currentGame = currentGame;
	}

	public static Game getCurrentGame()
	{
		return currentGame;
	}

	public static QSRFile getCurrentLoadQSR()
	{
		return Questor.loadedQSR;
	}

	public static void setCurrentLoadQSR(QSRFile qsr)
	{
		Questor.loadedQSR = qsr;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) 
	{
	    super.onCreateOptionsMenu(menu);
	    
	    this.menu  = menu;
	    
	    itemStart = menu.add(0, 0, 1, "Start Story");
	    itemStart.setIcon(android.R.drawable.ic_menu_mapmode);
	    itemStart.setOnMenuItemClickListener(this);
	    itemLoad = menu.add(0, 0, 2, "Continue Story");
	    itemLoad.setIcon(android.R.drawable.ic_menu_agenda);
	    itemLoad.setOnMenuItemClickListener(this);

	    itemSettings = menu.add(0, 1, 3, "Preferences");
	    itemSettings.setIcon(android.R.drawable.ic_menu_preferences);
	    itemSettings.setOnMenuItemClickListener(this);
	    itemClose = menu.add(0, 2, 4, "Exit");
	    itemClose.setIcon(android.R.drawable.ic_menu_close_clear_cancel);
	    itemClose.setOnMenuItemClickListener(this);
	    
	    return true;
	}
	
	@Override
	public void onClick(View v)
	{
		/*
		if (v.equals(menuButton))
			// FIXME: make menu visible
 	    */
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		if (requestCode==REQ_STORYSELECT) {
			// start quest
			currentGame = new Game(this);
			String storyUrl = data.getStringExtra(StorySelectorActivity.EXTRA_URL_STRING);
			startGame(currentGame, storyUrl);
		}
		else if (requestCode==REQ_STORYLOAD) {
			// load quest
			currentGame = new Game(this);
			try	{				
				String storyUrl = data.getStringExtra(StorySelectorActivity.EXTRA_URL_STRING);
				currentGame.loadGame(new URL(storyUrl));
				// FIXME: load player
			} catch (Exception e) {
				throw new RuntimeException("Error loading game from story url.", e);
			}
			
			// start StoryActivity
			Intent storyActivity = new Intent(this, StoryActivity.class);
			this.startActivity(storyActivity);
		}

	}
	
	@Override
	public boolean onMenuItemClick(MenuItem item)
	{
		if (item.equals(itemStart))
		{
			// select quest
			storySelectorActivity  = new Intent(this, StorySelectorActivity.class);
			this.startActivityForResult(storySelectorActivity, REQ_STORYSELECT);
						
			return true;
		}
		else if (item.equals(itemLoad))
		{
			// select stored quest
			storedStorySelectorActivity  = new Intent(this, StoredStorySelectorActivity.class);
			this.startActivityForResult(storedStorySelectorActivity, REQ_STORYLOAD);

			return true;
		}
		else if (item.equals(itemSettings))
		{
			// settings
			Intent settingsActivity = new Intent(this, SettingsActivity.class);
			this.startActivity(settingsActivity);

			return true;
		}
		else if (item.equals(itemClose))
		{
			this.finish();
			return true;
		}
		
		return false;
	}

	protected void startGame(final Game game, final String url) {
		try	{
			// load game file
			currentGame.loadGame(new URL(url));

			// start StoryActivity
			Intent storyActivity = new Intent(Questor.this, StoryActivity.class);
			Questor.this.startActivity(storyActivity);
		}
		catch (Exception e) {
			throw new RuntimeException("Error loading game from story url.", e);
		} 
		/*
		// display spinner
		final ProgressDialog pdlg = ProgressDialog.show(this, "Please wait..", "Opening Story..");
		new Thread() {
			public void run()
			{
				try
				{
					// load game file
					currentGame.loadGame(new URL(url));

					// start StoryActivity
					Intent storyActivity = new Intent(Questor.this, StoryActivity.class);
					Questor.this.startActivity(storyActivity);
				}
				catch (MalformedURLException e)
				{
					throw new RuntimeException("Error loading game from story url.", e);
				}
				// dismiss spinner
				pdlg.dismiss();
			}
       }.start();
       */ 
	}
}